package com.lz.ccjdemo.act;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lz.ccjdemo.R;
import com.lz.ccjdemo.utils.listener.PerfectClickListener;

import java.util.ArrayList;
import java.util.List;

/**********************************************************************
 * @desc 动态表头
 * @author lz
 * @data 2024/2/29 or 15:40
 *====================================================================*/
public class DynamicFormsActivity extends Activity {
    LinearLayout layout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_forms);
        layout = (LinearLayout) findViewById(R.id.layout_forms);
    }

    public void clickAdd(View view) {
        View itemForms = LayoutInflater.from(this).inflate(R.layout.item_forms, null);
        TextView btnDelete = itemForms.findViewById(R.id.btn_delete);
        TextView btnAdd = itemForms.findViewById(R.id.btn_add);
        LinearLayout layout1 = itemForms.findViewById(R.id.layout_forms2);
        btnAdd.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                addForms2(layout1);
            }
        });
        btnDelete.setOnClickListener(new PerfectClickListener() { //防抖操作
            @Override
            protected void onNoDoubleClick(View v) {
                layout.removeView(itemForms);
            }
        });
        layout.addView(itemForms);
    }

    private void addForms2(LinearLayout layoutView) {
        View itemForms2 = LayoutInflater.from(this).inflate(R.layout.item_forms_datlis, null);
        TextView btnDelete = itemForms2.findViewById(R.id.btn_delete);
        layoutView.addView(itemForms2);
        btnDelete.setOnClickListener(new PerfectClickListener() { //防抖操作
            @Override
            protected void onNoDoubleClick(View v) {
                layoutView.removeView(itemForms2);
            }
        });
    }

    public class PrlData {
        public String name;
        public String weight;
        public List<ItemData> list;

    }

    public class ItemData {
        public String name;
        public String weight;
    }

    /**
     * 读取所有添加的参数
     */
    public void prl() {
        int count = layout.getChildCount();
        List<PrlData> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            View view = layout.getChildAt(i);
            TextView tvNameTag = (TextView) view.findViewById(R.id.tv_name_tag);
            EditText etName = (EditText) view.findViewById(R.id.et_name);
            EditText etWeight = (EditText) view.findViewById(R.id.et_weight);
            PrlData prlData = new PrlData();
            prlData.name = etName.getText().toString();
            prlData.weight = etWeight.getText().toString();

            LinearLayout layout2 = view.findViewById(R.id.layout_forms2);
            prl2(prlData, layout2);
            list.add(prlData);
        }
    }

    private void prl2(PrlData prlData, LinearLayout layout2) {
        int count2 = layout2.getChildCount();
        if (count2 > 0) {
            //有二级内容
            List<ItemData> list = new ArrayList<>();
            for (int j = 0; j < count2; j++) {
                View view2 = layout2.getChildAt(j);
                EditText etName2 = (EditText) view2.findViewById(R.id.et_name);
                EditText etWeight2 = (EditText) view2.findViewById(R.id.et_weight);
                ItemData itemData = new ItemData();
                itemData.name = etName2.getText().toString();
                itemData.weight = etWeight2.getText().toString();
                list.add(itemData);
            }
            prlData.list = list;
        }
    }
}
